package com.example.controller;

import com.graduation.dto.OrganizationDO;
import com.graduation.mapper.OrganizationMapper;
import com.graduation.pojo.*;
import com.graduation.pojo.OrganizationQuery;
import com.graduation.pojo.OrganizationRequest;
import com.graduation.vo.OrganizationVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.stream.Collectors;

@Controller
@WebException
@RequestMapping("/organization")
public class OrganizationController extends BaseController<OrganizationDO, OrganizationVO> {

    @Resource
    private OrganizationMapper organizationMapper;

    @ResponseBody
    @GetMapping("/get")
        public WebResult<OrganizationVO> get(Integer id) {
        return new WebResult<>(getVoByDo(organizationMapper.selectById(id), OrganizationVO.class));
    }

    @ResponseBody
    @PostMapping("/page")
    public WebResult<PageResult<OrganizationVO>> page(OrganizationQuery query) {
        PageResult<OrganizationVO> result = new PageResult<>();
        result.setRowCount(organizationMapper.selectCount(query));
        result.setPageCount((result.getRowCount() - 1) / query.getPageSize() + 1);
        if (query.getPageNum() > result.getPageCount()) {
            query.setPageNum(result.getPageCount());
        }
        result.setPageNum(query.getPageNum());
        // 设置分页查询偏移量
        query.setLimit((query.getPageNum() - 1) * query.getPageSize());
        result.setEntityList(organizationMapper.selectList(query).stream()
                .map(e -> getVoByDo(e, OrganizationVO.class))
                .collect(Collectors.toList()));
        return new WebResult<>(result);
    }

    @ResponseBody
    @PostMapping("/operate")
    public WebResult<?> operate(HttpServletRequest servletRequest, OrganizationRequest request) {
        OrganizationDO organizationDO = getDoByRequest(request, OrganizationDO.class);
        if (Objects.isNull(organizationDO.getId())) {
            organizationDO.setCreateTime(LocalDateTime.now());
            organizationDO.setUpdateTime(LocalDateTime.now());
            organizationMapper.insert(organizationDO);
        } else {
            if (Objects.equals(request.getId(), request.getFatherOrg())) {
                return WebResult.failed("父子机构不能相同");
            }
            organizationDO.setUpdateTime(LocalDateTime.now());
            organizationMapper.updateById(organizationDO);
        }
        return WebResult.success();
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public WebResult<?> delete(Integer id) {
        organizationMapper.deleteById(id);
        return WebResult.success();
    }

    @RequestMapping("/to-organization")
    public String toOrganization() {
        return "organization/organizationAdmin";
    }

    @RequestMapping("/to-operate")
    public String toOperate(Model model, Integer id) {
        model.addAttribute("id", id);
        model.addAttribute("organization", Objects.isNull(id) ? new OrganizationDO(): organizationMapper.selectById(id));
        model.addAttribute("organizationList", organizationMapper.selectList(new OrganizationQuery()));
        return "organization/operateOrganization";
    }
}
